<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li class="active"><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 什么是服务器端渲染">1. 什么是服务器端渲染</a><ul><li><a href="#t11.1 服务器端渲染">1.1 服务器端渲染</a></li><li><a href="#t21.2 客户端渲染">1.2 客户端渲染</a></li></ul></li><li><a href="#t32. 服务器端打包React组件">2. 服务器端打包React组件</a><ul><li><a href="#t42.1 服务器端渲染React组件流程">2.1 服务器端渲染React组件流程</a></li><li><a href="#t52.2 webpack.config.js">2.2 webpack.config.js</a></li><li><a href="#t62.3 server/src/index.js">2.3 server/src/index.js</a></li><li><a href="#t72.4 Home/index.js">2.4 Home/index.js</a></li></ul></li><li><a href="#t83. 服务器端渲染">3. 服务器端渲染</a><ul><li><a href="#t93.1  server/webpack.config.js">3.1  server/webpack.config.js</a></li><li><a href="#t103.2 server/src/index.js">3.2 server/src/index.js</a></li><li><a href="#t113.3 package.json">3.3 package.json</a></li></ul></li><li><a href="#t124. 优化启动流程">4. 优化启动流程</a><ul><li><a href="#t134.1 安装nodemon">4.1 安装nodemon</a></li><li><a href="#t144.2 package.json">4.2 package.json</a></li><li><a href="#t154.3 npm-run-all">4.3 npm-run-all</a></li></ul></li><li><a href="#t165. 计数器组件">5. 计数器组件</a><ul><li><a href="#t175.1 src/client/index.js">5.1 src/client/index.js</a></li><li><a href="#t185.2 Counter/index.js">5.2 Counter/index.js</a></li><li><a href="#t195.3 webpack.client.js">5.3 webpack.client.js</a></li></ul></li><li><a href="#t206. 优化代码结构">6. 优化代码结构</a><ul><li><a href="#t216.1 webpack.base.js">6.1 webpack.base.js</a></li><li><a href="#t226.2 webpack.client.js">6.2 webpack.client.js</a></li><li><a href="#t236.3 webpack.server.js">6.3 webpack.server.js</a></li><li><a href="#t246.4 server/index.js">6.4 server/index.js</a></li></ul></li><li><a href="#t257. 使用路由">7. 使用路由</a><ul><li><a href="#t267.1 客户端路由">7.1 客户端路由</a></li><li><a href="#t277.2 src/client/index.js">7.2 src/client/index.js</a></li><li><a href="#t287.3 src/server/index.js">7.3 src/server/index.js</a></li><li><a href="#t297.4 src/routes.js">7.4 src/routes.js</a></li></ul></li><li><a href="#t308. 跳转路由">8. 跳转路由</a><ul><li><a href="#t318.1 src/client/index.js">8.1 src/client/index.js</a></li><li><a href="#t328.2 /Counter/index.js">8.2 /Counter/index.js</a></li><li><a href="#t338.3 Home/index.js">8.3 Home/index.js</a></li><li><a href="#t348.4 src/server/index.js">8.4 src/server/index.js</a></li><li><a href="#t358.5 /Header/index.js">8.5 /Header/index.js</a></li><li><a href="#t368.6 src/server/render.js">8.6 src/server/render.js</a></li></ul></li><li><a href="#t379. redux计数器">9. redux计数器</a><ul><li><a href="#t389.1 src/client/index.js">9.1 src/client/index.js</a></li><li><a href="#t399.2 Counter/index.js">9.2 Counter/index.js</a></li><li><a href="#t409.3 server/render.js">9.3 server/render.js</a></li><li><a href="#t419.4 src/store/index.js">9.4 src/store/index.js</a></li><li><a href="#t429.5 store/reducer.js">9.5 store/reducer.js</a></li><li><a href="#t439.6 action-types.js">9.6 action-types.js</a></li><li><a href="#t449.7 store/actions.js">9.7 store/actions.js</a></li></ul></li><li><a href="#t4510. 合并reducers">10. 合并reducers</a><ul><li><a href="#t4610.1 Counter/index.js">10.1 Counter/index.js</a></li><li><a href="#t4710.2 src/store/index.js">10.2 src/store/index.js</a></li><li><a href="#t4810.3 reducers/index.js">10.3 reducers/index.js</a></li><li><a href="#t4910.4 reducers/home.js">10.4 reducers/home.js</a></li><li><a href="#t5010.5 reducers/counter.js">10.5 reducers/counter.js</a></li></ul></li><li><a href="#t5111. 客户端加载数据">11. 客户端加载数据</a><ul><li><a href="#t5211.1 src/client/index.js">11.1 src/client/index.js</a></li><li><a href="#t5311.2 Counter/index.js">11.2 Counter/index.js</a></li><li><a href="#t5411.3 Home/index.js">11.3 Home/index.js</a></li><li><a href="#t5511.3 Home/index.js">11.3 Home/index.js</a></li><li><a href="#t5611.4 src/server/render.js">11.4 src/server/render.js</a></li><li><a href="#t5711.5 src/store/action-types.js">11.5 src/store/action-types.js</a></li><li><a href="#t5811.6 reducers/home.js">11.6 reducers/home.js</a></li><li><a href="#t5911.7  actions/counter.js">11.7  actions/counter.js</a></li><li><a href="#t6011.8 reducers/home.js">11.8 reducers/home.js</a></li><li><a href="#t6111.9 api/server.js">11.9 api/server.js</a></li></ul></li><li><a href="#t6212. 服务器端路由">12. 服务器端路由</a><ul><li><a href="#t6312.1 src/client/index.js">12.1 src/client/index.js</a></li><li><a href="#t6412.2 Home/index.js">12.2 Home/index.js</a></li><li><a href="#t6512.3 src/routes.js">12.3 src/routes.js</a></li><li><a href="#t6612.4 src/server/render.js">12.4 src/server/render.js</a></li></ul></li><li><a href="#t6713.多级路由">13.多级路由</a><ul><li><a href="#t6813.1 src/client/index.js">13.1 src/client/index.js</a></li><li><a href="#t6913.2 Header/index.js">13.2 Header/index.js</a></li><li><a href="#t7013.3 src/routes.js">13.3 src/routes.js</a></li><li><a href="#t7113.4 src/server/render.js">13.4 src/server/render.js</a></li><li><a href="#t7213.5 src/server/render.js">13.5 src/server/render.js</a></li><li><a href="#t7313.6 components/UserList.js">13.6 components/UserList.js</a></li></ul></li><li><a href="#t7414.后台获取数据">14.后台获取数据</a><ul><li><a href="#t7514.1 src/client/index.js">14.1 src/client/index.js</a></li><li><a href="#t7614.2 Home/index.js">14.2 Home/index.js</a></li><li><a href="#t7714.3 src/server/render.js">14.3 src/server/render.js</a></li><li><a href="#t7814.4 actions/home.js">14.4 actions/home.js</a></li><li><a href="#t7914.5 src/store/index.js">14.5 src/store/index.js</a></li></ul></li><li><a href="#t8015. Node代理服务器">15. Node代理服务器</a><ul><li><a href="#t8115.1 src/server/index.js">15.1 src/server/index.js</a></li><li><a href="#t8215.2 src/server/render.js">15.2 src/server/render.js</a></li><li><a href="#t8315.3 actions/home.js">15.3 actions/home.js</a></li><li><a href="#t8415.4 src/store/index.js">15.4 src/store/index.js</a></li><li><a href="#t8515.5 src/client/request.js">15.5 src/client/request.js</a></li><li><a href="#t8615.6 src/server/request.js">15.6 src/server/request.js</a></li></ul></li><li><a href="#t8716. 抽取App.js">16. 抽取App.js</a><ul><li><a href="#t8816.1 src/client/index.js">16.1 src/client/index.js</a></li><li><a href="#t8916.2 User/index.js">16.2 User/index.js</a></li><li><a href="#t9016.3 src/routes.js">16.3 src/routes.js</a></li><li><a href="#t9116.4 src/server/index.js">16.4 src/server/index.js</a></li><li><a href="#t9216.5 src/server/render.js">16.5 src/server/render.js</a></li><li><a href="#t9316.6  src/containers/App.js">16.6  src/containers/App.js</a></li></ul></li><li><a href="#t9417. 实现头部导航">17. 实现头部导航</a><ul><li><a href="#t9517.1 src/client/index.js">17.1 src/client/index.js</a></li><li><a href="#t9617.2 src/components/Header/index.js">17.2 src/components/Header/index.js</a></li><li><a href="#t9717.3 src/store/action-types.js">17.3 src/store/action-types.js</a></li><li><a href="#t9817.4 reducers/index.js">17.4 reducers/index.js</a></li><li><a href="#t9917.5  reducers/session.js">17.5  reducers/session.js</a></li></ul></li><li><a href="#t10018. 实现登录功能">18. 实现登录功能</a><ul><li><a href="#t10118.1 api/server.js">18.1 api/server.js</a></li><li><a href="#t10218.2 Header/index.js">18.2 Header/index.js</a></li><li><a href="#t10318.3 src/containers/App.js">18.3 src/containers/App.js</a></li><li><a href="#t10418.4 src/routes.js">18.4 src/routes.js</a></li><li><a href="#t10518.5 src/server/index.js">18.5 src/server/index.js</a></li><li><a href="#t10618.6 action-types.js">18.6 action-types.js</a></li><li><a href="#t10718.7 reducers/session.js">18.7 reducers/session.js</a></li><li><a href="#t10818.7 Login/index.js">18.7 Login/index.js</a></li><li><a href="#t10918.8 Logout/index.js">18.8 Logout/index.js</a></li><li><a href="#t11018.9 Profile/index.js">18.9 Profile/index.js</a></li><li><a href="#t11118.10 actions/session.js">18.10 actions/session.js</a></li></ul></li><li><a href="#t11219. 退出功能">19. 退出功能</a><ul><li><a href="#t11319.1 Logout/index.js">19.1 Logout/index.js</a></li><li><a href="#t11419.2 actions/session.js">19.2 actions/session.js</a></li></ul></li><li><a href="#t11520. 加载用户信息">20. 加载用户信息</a><ul><li><a href="#t11620.1 api/server.js">20.1 api/server.js</a></li><li><a href="#t11720.2 containers/App.js">20.2 containers/App.js</a></li><li><a href="#t11820.3 Profile/index.js">20.3 Profile/index.js</a></li><li><a href="#t11920.4 src/routes.js">20.4 src/routes.js</a></li><li><a href="#t12020.5 src/server/render.js">20.5 src/server/render.js</a></li><li><a href="#t12120.6 src/server/request.js">20.6 src/server/request.js</a></li><li><a href="#t12220.7 actions/session.js">20.7 actions/session.js</a></li><li><a href="#t12320.7 src/store/index.js">20.7 src/store/index.js</a></li></ul></li><li><a href="#t12421. 404">21. 404</a><ul><li><a href="#t12521.1 src/routes.js">21.1 src/routes.js</a></li><li><a href="#t12621.2 src/server/render.js">21.2 src/server/render.js</a></li><li><a href="#t12721.3 NotFound/index.js">21.3 NotFound/index.js</a></li></ul></li><li><a href="#t12822. 301">22. 301</a><ul><li><a href="#t12922.1 server/render.js">22.1 server/render.js</a></li></ul></li><li><a href="#t13023. promise.all">23. promise.all</a><ul><li><a href="#t13123.1 src/server/render.js">23.1 src/server/render.js</a></li></ul></li><li><a href="#t13224. 使用CSS">24. 使用CSS</a><ul><li><a href="#t13324.1 Header/index.js">24.1 Header/index.js</a></li><li><a href="#t13424.2 src/containers/App.js">24.2 src/containers/App.js</a></li><li><a href="#t13524.3 webpack.client.js">24.3 webpack.client.js</a></li><li><a href="#t13624.4 webpack.server.js">24.4 webpack.server.js</a></li><li><a href="#t13724.5 Header/index.css">24.5 Header/index.css</a></li><li><a href="#t13824.6 src/containers/App.css">24.6 src/containers/App.css</a></li></ul></li><li><a href="#t13925. CSS服务器端渲染">25. CSS服务器端渲染</a><ul><li><a href="#t14025.1 Header/index.js">25.1 Header/index.js</a></li><li><a href="#t14125.2 containers/App.js">25.2 containers/App.js</a></li><li><a href="#t14225.3 server/render.js">25.3 server/render.js</a></li></ul></li><li><a href="#t14326. 优化CSS服务器端渲染">26. 优化CSS服务器端渲染</a><ul><li><a href="#t14426.1 Header/index.js">26.1 Header/index.js</a></li><li><a href="#t14526.2 src/containers/App.js">26.2 src/containers/App.js</a></li><li><a href="#t14626.3 Home/index.js">26.3 Home/index.js</a></li><li><a href="#t14726.4 src/withStyles.js">26.4 src/withStyles.js</a></li></ul></li><li><a href="#t14827. SEO优化">27. SEO优化</a><ul><li><a href="#t14927.1 Home/index.js">27.1 Home/index.js</a></li><li><a href="#t15027.2 src/server/render.js">27.2 src/server/render.js</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x4EC0;&#x4E48;&#x662F;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;">1. &#x4EC0;&#x4E48;&#x662F;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3; <a href="#t01. &#x4EC0;&#x4E48;&#x662F;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;"> # </a></h2>
<h3 id="t11.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;">1.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3; <a href="#t11.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;"> # </a></h3>
<p>&#x9875;&#x9762;&#x4E0A;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x7531;&#x670D;&#x52A1;&#x5668;&#x751F;&#x4EA7;&#x7684;</p>
<pre><code class="lang-js">cnpm i express -S
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
app.get(<span class="hljs-string">&apos;/&apos;</span>,(req,res) =&gt; {
    res.send(<span class="hljs-string">`
        &lt;html&gt;
          &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;hello&lt;/div&gt;
          &lt;/body&gt;
        &lt;/html&gt;
    `</span>);
});
app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h3 id="t21.2 &#x5BA2;&#x6237;&#x7AEF;&#x6E32;&#x67D3;">1.2 &#x5BA2;&#x6237;&#x7AEF;&#x6E32;&#x67D3; <a href="#t21.2 &#x5BA2;&#x6237;&#x7AEF;&#x6E32;&#x67D3;"> # </a></h3>
<p>&#x9875;&#x9762;&#x4E0A;&#x7684;&#x5185;&#x5BB9;&#x7531;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x8FD0;&#x884C;JS&#x811A;&#x672C;&#x800C;&#x6E32;&#x67D3;&#x5230;&#x9875;&#x9762;&#x4E0A;&#x7684;</p>
<ul>
<li>&#x6D4F;&#x89C8;&#x5668;&#x8BBF;&#x95EE;&#x670D;&#x52A1;&#x5668;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7A7A;&#x7684;HTML&#x9875;&#x9762;&#xFF0C;&#x91CC;&#x9762;&#x6709;&#x4E00;&#x4E2A;JS&#x8D44;&#x6E90;&#x94FE;&#x63A5;&#xFF0C;&#x6BD4;&#x5982;bundle.js</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x4E0B;&#x8F7D;JS&#x4EE3;&#x7801;&#x5E76;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x8FD0;&#x884C;</li>
<li>&#x5185;&#x5BB9;&#x5448;&#x73B0;&#x5728;&#x9875;&#x9762;&#x4E0A;</li>
</ul>
<pre><code class="lang-js">cnpm i react react-dom -S
cnpm i webpack webpack-cli -D
cnpm i babel-loader @babel/core -D
cnpm i @babel/preset-env @babel/preset-react  -D
cnpm i webpack-node-externals -D
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
app.get(<span class="hljs-string">&apos;/&apos;</span>,(req,res) =&gt; {
    res.send(<span class="hljs-string">`
        &lt;html&gt;
          &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
            &lt;script&gt;
                document.getElementById(&apos;root&apos;).innerHTML = &apos;hello&apos;;
            &lt;/script&gt;
          &lt;/body&gt;
        &lt;/html&gt;
    `</span>);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h2 id="t32. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6253;&#x5305;React&#x7EC4;&#x4EF6;">2. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6253;&#x5305;React&#x7EC4;&#x4EF6; <a href="#t32. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6253;&#x5305;React&#x7EC4;&#x4EF6;"> # </a></h2>
<h3 id="t42.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;React&#x7EC4;&#x4EF6;&#x6D41;&#x7A0B;">2.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;React&#x7EC4;&#x4EF6;&#x6D41;&#x7A0B; <a href="#t42.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;React&#x7EC4;&#x4EF6;&#x6D41;&#x7A0B;"> # </a></h3>
<ul>
<li><ol>
<li>&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x9001;&#x8BF7;&#x6C42;</li>
</ol>
</li>
<li><ol start="2">
<li>&#x670D;&#x52A1;&#x5668;&#x8FD0;&#x884C;React&#x4EE3;&#x7801;&#x751F;&#x6210;&#x9875;&#x9762;</li>
</ol>
</li>
<li><ol start="3">
<li>&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x9875;&#x9762;</li>
</ol>
</li>
</ul>
<h3 id="t52.2 webpack.config.js">2.2 webpack.config.js <a href="#t52.2 webpack.config.js"> # </a></h3>
<p>server/webpack.config.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668; &#x670D;&#x52A1;&#x5668;&#x7AEF;</span>
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> nodeExternals=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack-node-externals&apos;</span>);
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">target</span>: <span class="hljs-string">&apos;node&apos;</span>,<span class="hljs-comment">//&#x6253;&#x5305;&#x7684;&#x662F;&#x670D;&#x52A1;&#x5668;&#x7AEF;node&#x6587;&#x4EF6;</span>
    mode:<span class="hljs-string">&apos;development&apos;</span>,<span class="hljs-comment">//&#x5F00;&#x53D1;&#x6A21;&#x5F0F;</span>
    output:{
        <span class="hljs-attr">path</span>:path.resolve(__dirname,<span class="hljs-string">&apos;build&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">externals</span>:[nodeExternals()],
    <span class="hljs-attr">module</span>:{
        <span class="hljs-attr">rules</span>:[
            {
                <span class="hljs-attr">test</span>:<span class="hljs-regexp">/\.js$/</span>,
                <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;babel-loader&apos;</span>,
                <span class="hljs-attr">exclude</span>: <span class="hljs-regexp">/node_modules/</span>,
                <span class="hljs-attr">options</span>: {
                    <span class="hljs-attr">presets</span>: [
                        [
                            <span class="hljs-string">&quot;@babel/preset-env&quot;</span>,{
                                <span class="hljs-attr">targets</span>: {
                                    <span class="hljs-attr">browsers</span>:[<span class="hljs-string">&apos;last 2 versions&apos;</span>]
                                }
                            }
                        ],<span class="hljs-string">&quot;@babel/preset-react&quot;</span>
                    ]
                }
            }
        ]
    }
}
</code></pre>
<h3 id="t62.3 server/src/index.js">2.3 server/src/index.js <a href="#t62.3 server/src/index.js"> # </a></h3>
<p>server/src/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
app.get(<span class="hljs-string">&apos;/&apos;</span>,(req,res) =&gt; {
    res.send(<span class="hljs-string">`
        &lt;html&gt;
          &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
            &lt;script&gt;
                document.getElementById(&apos;root&apos;).innerHTML = &apos;hello&apos;;
            &lt;/script&gt;
          &lt;/body&gt;
        &lt;/html&gt;
    `</span>);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t72.4 Home/index.js">2.4 Home/index.js <a href="#t72.4 Home/index.js"> # </a></h3>
<p>server/src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    }
}
</code></pre>
<h2 id="t83. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;">3. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3; <a href="#t83. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;"> # </a></h2>
<h3 id="t93.1  server/webpack.config.js">3.1  server/webpack.config.js <a href="#t93.1  server/webpack.config.js"> # </a></h3>
<p>server/webpack.config.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668; &#x670D;&#x52A1;&#x5668;&#x7AEF;</span>
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> nodeExternals=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack-node-externals&apos;</span>);
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">target</span>: <span class="hljs-string">&apos;node&apos;</span>,<span class="hljs-comment">//&#x6253;&#x5305;&#x7684;&#x662F;&#x670D;&#x52A1;&#x5668;&#x7AEF;node&#x6587;&#x4EF6;</span>
    mode: <span class="hljs-string">&apos;development&apos;</span>,<span class="hljs-comment">//&#x5F00;&#x53D1;&#x6A21;&#x5F0F;</span>
    entry:path.resolve(__dirname,<span class="hljs-string">&apos;./src/index.js&apos;</span>),
    <span class="hljs-attr">output</span>:{
        <span class="hljs-attr">path</span>:path.resolve(__dirname,<span class="hljs-string">&apos;build&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">externals</span>:[nodeExternals()],
    <span class="hljs-attr">module</span>:{
        <span class="hljs-attr">rules</span>:[
            {
                <span class="hljs-attr">test</span>:<span class="hljs-regexp">/\.js$/</span>,
                <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;babel-loader&apos;</span>,
                <span class="hljs-attr">exclude</span>: <span class="hljs-regexp">/node_modules/</span>,
                <span class="hljs-attr">options</span>: {
                    <span class="hljs-attr">presets</span>: [
                        <span class="hljs-string">&quot;@babel/preset-env&quot;</span>,
                        <span class="hljs-string">&quot;@babel/preset-react&quot;</span>
                    ]
                }
            }
        ]
    }
}
</code></pre>
<h3 id="t103.2 server/src/index.js">3.2 server/src/index.js <a href="#t103.2 server/src/index.js"> # </a></h3>
<p>server/src/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
<span class="hljs-keyword">const</span> html=renderToString(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span>);
app.get(&apos;/&apos;,(req,res) =&gt; {
    res.send(`
    <span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span>
          ${html}
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
    `);
});
app.listen(9090);
</span></code></pre>
<h3 id="t113.3 package.json">3.3 package.json <a href="#t113.3 package.json"> # </a></h3>
<p>package.json</p>
<pre><code class="lang-json">&quot;scripts&quot;: {
    &quot;start&quot;: &quot;node ./server/build/bundle.js&quot;,
    &quot;build&quot;:&quot;webpack --config server/webpack.config.js&quot;
}
</code></pre>
<h2 id="t124. &#x4F18;&#x5316;&#x542F;&#x52A8;&#x6D41;&#x7A0B;">4. &#x4F18;&#x5316;&#x542F;&#x52A8;&#x6D41;&#x7A0B; <a href="#t124. &#x4F18;&#x5316;&#x542F;&#x52A8;&#x6D41;&#x7A0B;"> # </a></h2>
<h3 id="t134.1 &#x5B89;&#x88C5;nodemon">4.1 &#x5B89;&#x88C5;nodemon <a href="#t134.1 &#x5B89;&#x88C5;nodemon"> # </a></h3>
<pre><code class="lang-js">cnpm i nodemon -g
</code></pre>
<h3 id="t144.2 package.json">4.2 package.json <a href="#t144.2 package.json"> # </a></h3>
<pre><code class="lang-json">&quot;scripts&quot;: {
    &quot;dev&quot;:&quot;npm-run-all --parallel dev:**&quot;,
    &quot;dev:start&quot;:&quot;nodemon &apos;./server/build/bundle.js&apos;&quot;,
    &quot;dev:build&quot;:&quot;webpack --config server/webpack.config.js --watch&quot;
}
</code></pre>
<h3 id="t154.3 npm-run-all">4.3 npm-run-all <a href="#t154.3 npm-run-all"> # </a></h3>
<pre><code class="lang-js">cnpm npm-run-all -g
</code></pre>
<h2 id="t165. &#x8BA1;&#x6570;&#x5668;&#x7EC4;&#x4EF6;">5. &#x8BA1;&#x6570;&#x5668;&#x7EC4;&#x4EF6; <a href="#t165. &#x8BA1;&#x6570;&#x5668;&#x7EC4;&#x4EF6;"> # </a></h2>
<ul>
<li>&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8FD0;&#x884C;React&#x4EE3;&#x7801;&#x6E32;&#x67D3;&#x51FA;HTML</li>
<li>&#x670D;&#x52A1;&#x5668;&#x628A;&#x6E32;&#x67D3;&#x51FA;&#x7684;HTML&#x9875;&#x9762;&#x53D1;&#x9001;&#x7ED9;&#x4E86;&#x6D4F;&#x89C8;&#x5668;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x63A5;&#x53D7;&#x5230;HTML&#x4F1A;&#x6E32;&#x67D3;&#x5230;&#x9875;&#x9762;&#x4E0A;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x73B0;&#x9875;&#x9762;&#x5F15;&#x7528;&#x7684;index.js&#x6587;&#x4EF6;&#x4F1A;&#x53BB;&#x4E0B;&#x8F7D;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x4E0B;&#x8F7D;&#x5F97;&#x5230;&#x7684;index.js&#x6587;&#x4EF6;&#x5E76;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x6267;&#x884C;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x63A5;&#x7BA1;&#x4E86;&#x9875;&#x9762;&#x7684;&#x6240;&#x6709;&#x5185;&#x5BB9;&#xFF0C;&#x540E;&#x9762;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#x6E32;&#x67D3;&#x662F;&#x4E00;&#x6837;&#x7684;</li>
</ul>
<h3 id="t175.1 src/client/index.js">5.1 src/client/index.js <a href="#t175.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/Counter&apos;</span>;

ReactDOM.hydrate(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Counter</span>/&gt;</span></span>,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t185.2 Counter/index.js">5.2 Counter/index.js <a href="#t185.2 Counter/index.js"> # </a></h3>
<p>src/containers/Counter/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    state={<span class="hljs-attr">number</span>:<span class="hljs-number">0</span>}
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.state.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span>=&gt;</span>this.setState({number:this.state.number+1})}&gt;+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t195.3 webpack.client.js">5.3 webpack.client.js <a href="#t195.3 webpack.client.js"> # </a></h3>
<p>webpack.client.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668; &#x670D;&#x52A1;&#x5668;&#x7AEF;</span>
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,<span class="hljs-comment">//&#x5F00;&#x53D1;&#x6A21;&#x5F0F;</span>
    entry:path.resolve(__dirname,<span class="hljs-string">&apos;./src/client/index.js&apos;</span>),
    <span class="hljs-attr">output</span>:{
        <span class="hljs-attr">path</span>:path.resolve(__dirname,<span class="hljs-string">&apos;public&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;index.js&apos;</span>
    },
    <span class="hljs-attr">module</span>:{
        <span class="hljs-attr">rules</span>:[
            {
                <span class="hljs-attr">test</span>:<span class="hljs-regexp">/\.js$/</span>,
                <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;babel-loader&apos;</span>,
                <span class="hljs-attr">exclude</span>: <span class="hljs-regexp">/node_modules/</span>,
                <span class="hljs-attr">options</span>: {
                    <span class="hljs-attr">presets</span>: [
                        <span class="hljs-string">&quot;@babel/preset-env&quot;</span>,
                        <span class="hljs-string">&quot;@babel/preset-react&quot;</span>
                    ],
                    <span class="hljs-attr">plugins</span>: [
                        <span class="hljs-string">&quot;@babel/plugin-proposal-class-properties&quot;</span>
                    ]
                }
            }
        ]
    }
}
</code></pre>
<h2 id="t206. &#x4F18;&#x5316;&#x4EE3;&#x7801;&#x7ED3;&#x6784;">6. &#x4F18;&#x5316;&#x4EE3;&#x7801;&#x7ED3;&#x6784; <a href="#t206. &#x4F18;&#x5316;&#x4EE3;&#x7801;&#x7ED3;&#x6784;"> # </a></h2>
<h3 id="t216.1 webpack.base.js">6.1 webpack.base.js <a href="#t216.1 webpack.base.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668; &#x670D;&#x52A1;&#x5668;&#x7AEF;</span>
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,
    <span class="hljs-attr">module</span>:{
        <span class="hljs-attr">rules</span>:[
            {
                <span class="hljs-attr">test</span>:<span class="hljs-regexp">/\.js$/</span>,
                <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;babel-loader&apos;</span>,
                <span class="hljs-attr">exclude</span>: <span class="hljs-regexp">/node_modules/</span>,
                <span class="hljs-attr">options</span>: {
                    <span class="hljs-attr">presets</span>: [
                        <span class="hljs-string">&quot;@babel/preset-env&quot;</span>,
                        <span class="hljs-string">&quot;@babel/preset-react&quot;</span>
                    ],
                    <span class="hljs-attr">plugins</span>: [
                        <span class="hljs-string">&quot;@babel/plugin-proposal-class-properties&quot;</span>
                    ]
                }
            }
        ]
    }
}
</code></pre>
<h3 id="t226.2 webpack.client.js">6.2 webpack.client.js <a href="#t226.2 webpack.client.js"> # </a></h3>
<p>webpack.client.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668; &#x670D;&#x52A1;&#x5668;&#x7AEF;</span>
<span class="hljs-keyword">let</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> merge=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack-merge&apos;</span>);
<span class="hljs-keyword">const</span> base=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./webpack.base&apos;</span>);
<span class="hljs-built_in">module</span>.exports = merge(base,{
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,<span class="hljs-comment">//&#x5F00;&#x53D1;&#x6A21;&#x5F0F;</span>
    entry:path.resolve(__dirname,<span class="hljs-string">&apos;./src/client/index.js&apos;</span>),
    <span class="hljs-attr">output</span>:{
        <span class="hljs-attr">path</span>:path.resolve(__dirname,<span class="hljs-string">&apos;public&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;index.js&apos;</span>
    }
}) 
</code></pre>
<h3 id="t236.3 webpack.server.js">6.3 webpack.server.js <a href="#t236.3 webpack.server.js"> # </a></h3>
<p>webpack.server.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668; &#x670D;&#x52A1;&#x5668;&#x7AEF;</span>
<span class="hljs-keyword">let</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> merge=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack-merge&apos;</span>);
<span class="hljs-keyword">const</span> base=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./webpack.base&apos;</span>);
<span class="hljs-keyword">const</span> nodeExternals=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack-node-externals&apos;</span>);
<span class="hljs-built_in">module</span>.exports = merge(base,{
    <span class="hljs-attr">target</span>: <span class="hljs-string">&apos;node&apos;</span>,<span class="hljs-comment">//&#x6253;&#x5305;&#x7684;&#x662F;&#x670D;&#x52A1;&#x5668;&#x7AEF;node&#x6587;&#x4EF6;</span>
    mode: <span class="hljs-string">&apos;development&apos;</span>,<span class="hljs-comment">//&#x5F00;&#x53D1;&#x6A21;&#x5F0F;</span>
    entry:path.resolve(__dirname,<span class="hljs-string">&apos;./src/server/index.js&apos;</span>),
    <span class="hljs-attr">output</span>:{
        <span class="hljs-attr">path</span>:path.resolve(__dirname,<span class="hljs-string">&apos;build&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">externals</span>:[nodeExternals()]
}) 
</code></pre>
<h3 id="t246.4 server/index.js">6.4 server/index.js <a href="#t246.4 server/index.js"> # </a></h3>
<p>src/server/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>));
<span class="hljs-keyword">const</span> content=renderToString(&lt;Home /&gt;);
app.get(&apos;/&apos;,(req,res) =&gt; {
    res.send(`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;title&gt;&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;${content}&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `);
});
app.listen(9090);
</code></pre>
<h2 id="t257. &#x4F7F;&#x7528;&#x8DEF;&#x7531;">7. &#x4F7F;&#x7528;&#x8DEF;&#x7531; <a href="#t257. &#x4F7F;&#x7528;&#x8DEF;&#x7531;"> # </a></h2>
<pre><code class="lang-js">cnpm i react-router-dom -S
</code></pre>
<h3 id="t267.1 &#x5BA2;&#x6237;&#x7AEF;&#x8DEF;&#x7531;">7.1 &#x5BA2;&#x6237;&#x7AEF;&#x8DEF;&#x7531; <a href="#t267.1 &#x5BA2;&#x6237;&#x7AEF;&#x8DEF;&#x7531;"> # </a></h3>
<ul>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x670D;&#x52A1;&#x5668;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;HTML&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x6E32;&#x67D3;&#x663E;&#x793A;&#x9875;&#x9762;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x73B0;&#x9700;&#x8981;&#x5916;&#x94FE;JS&#x8D44;&#x6E90;&#xFF0C;&#x52A0;&#x8F7D;JS&#x8D44;&#x6E90;</li>
<li>&#x52A0;&#x8F7D;&#x597D;&#x7684;JS&#x8D44;&#x6E90;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x6267;&#x884C;</li>
<li>JS&#x4E2D;&#x7684;Reac&#x4EE3;&#x7801;&#x5F00;&#x59CB;&#x5B9E;&#x73B0;&#x8DEF;&#x7531;&#x529F;&#x80FD;</li>
<li>&#x8DEF;&#x7531;&#x4EE3;&#x7801;&#x9996;&#x5148;&#x83B7;&#x53D6;&#x5730;&#x5740;&#x680F;&#x4E2D;&#x7684;&#x5730;&#x5740;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x5730;&#x5740;&#x6839;&#x636E;&#x8DEF;&#x7531;&#x914D;&#x7F6E;&#x6E32;&#x67D3;&#x5BF9;&#x5E94;&#x5185;&#x5BB9;</li>
</ul>
<h3 id="t277.2 src/client/index.js">7.2 src/client/index.js <a href="#t277.2 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
        {routes}
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t287.3 src/server/index.js">7.3 src/server/index.js <a href="#t287.3 src/server/index.js"> # </a></h3>
<p>src/server/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>));
<span class="hljs-comment">//context&#x6570;&#x636E;&#x7684;&#x4F20;&#x9012; StaticRouter&#x9700;&#x8981;&#x77E5;&#x9053;&#x5F53;&#x524D;&#x8DEF;&#x5F84;</span>

app.get(<span class="hljs-string">&apos;*&apos;</span>,(req,res) =&gt; {
    <span class="hljs-keyword">const</span> content=renderToString(
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
            {routes}
        <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span></span>
    );
    res.send(<span class="hljs-string">`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;title&gt;&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `</span>);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t297.4 src/routes.js">7.4 src/routes.js <a href="#t297.4 src/routes.js"> # </a></h3>
<p>src/routes.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Route} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/&quot;</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/counter&quot;</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Counter}</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
)
</code></pre>
<h2 id="t308. &#x8DF3;&#x8F6C;&#x8DEF;&#x7531;">8. &#x8DF3;&#x8F6C;&#x8DEF;&#x7531; <a href="#t308. &#x8DF3;&#x8F6C;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t318.1 src/client/index.js">8.1 src/client/index.js <a href="#t318.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:50}}</span>&gt;</span>
                {routes}
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t328.2 /Counter/index.js">8.2 /Counter/index.js <a href="#t328.2 /Counter/index.js"> # </a></h3>
<p>src/containers/Counter/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    state={<span class="hljs-attr">number</span>:<span class="hljs-number">0</span>}
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.state.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span>=&gt;</span>this.setState({number:this.state.number+1})}&gt;+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t338.3 Home/index.js">8.3 Home/index.js <a href="#t338.3 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    }
}
</code></pre>
<h3 id="t348.4 src/server/index.js">8.4 src/server/index.js <a href="#t348.4 src/server/index.js"> # </a></h3>
<p>src/server/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">import</span> render <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./render&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>));
<span class="hljs-comment">//context&#x6570;&#x636E;&#x7684;&#x4F20;&#x9012; StaticRouter&#x9700;&#x8981;&#x77E5;&#x9053;&#x5F53;&#x524D;&#x8DEF;&#x5F84;</span>
app.get(<span class="hljs-string">&apos;*&apos;</span>,(req,res) =&gt; {
    render(req,res);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t358.5 /Header/index.js">8.5 /Header/index.js <a href="#t358.5 /Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-inverse navbar-fixed-top&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;SSR<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;navbar&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;collapse navbar-collapse&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/counter&quot;</span>&gt;</span>Counter<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t368.6 src/server/render.js">8.6 src/server/render.js <a href="#t368.6 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">const</span> content=renderToString(
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:50}}</span>&gt;</span>
                        {routes}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span></span>
    );
    res.send(<span class="hljs-string">`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7d5d8d8c3c4c3c5d6c7f78499849980">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
            &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `</span>);
}
</code></pre>
<h2 id="t379. redux&#x8BA1;&#x6570;&#x5668;">9. redux&#x8BA1;&#x6570;&#x5668; <a href="#t379. redux&#x8BA1;&#x6570;&#x5668;"> # </a></h2>
<h3 id="t389.1 src/client/index.js">9.1 src/client/index.js <a href="#t389.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:50}}</span>&gt;</span>
                    {routes}
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t399.2 Counter/index.js">9.2 Counter/index.js <a href="#t399.2 Counter/index.js"> # </a></h3>
<p>src/containers/Counter/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.props.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.increment}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state,
    actions
)(Counter);
</code></pre>
<h3 id="t409.3 server/render.js">9.3 server/render.js <a href="#t409.3 server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">const</span> content=renderToString(
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getStore()}</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:50}}</span>&gt;</span>
                            {routes}
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    );
    res.send(<span class="hljs-string">`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096b66667d7a7d7b6879493a273a273e">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
            &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `</span>);
}
</code></pre>
<h3 id="t419.4 src/store/index.js">9.4 src/store/index.js <a href="#t419.4 src/store/index.js"> # </a></h3>
<p>src/store/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> reducer <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducer&apos;</span>;
<span class="hljs-keyword">import</span> {createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> thunk <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-thunk&apos;</span>;
<span class="hljs-keyword">import</span> logger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> createStore(reducer,applyMiddleware(thunk,logger));
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> getStore;
</code></pre>
<h3 id="t429.5 store/reducer.js">9.5 store/reducer.js <a href="#t429.5 store/reducer.js"> # </a></h3>
<p>src/store/reducer.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState = {
    <span class="hljs-attr">number</span>: <span class="hljs-number">0</span>
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.INCREMENT:
            <span class="hljs-keyword">return</span> {
                <span class="hljs-attr">number</span>: state.number + <span class="hljs-number">1</span>
            };
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t439.6 action-types.js">9.6 action-types.js <a href="#t439.6 action-types.js"> # </a></h3>
<p>src/store/action-types.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT=<span class="hljs-string">&apos;INCREMENT&apos;</span>;
</code></pre>
<h3 id="t449.7 store/actions.js">9.7 store/actions.js <a href="#t449.7 store/actions.js"> # </a></h3>
<p>src/store/actions.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    increment() {
        <span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:types.INCREMENT}
    }
}
</code></pre>
<h2 id="t4510. &#x5408;&#x5E76;reducers">10. &#x5408;&#x5E76;reducers <a href="#t4510. &#x5408;&#x5E76;reducers"> # </a></h2>
<h3 id="t4610.1 Counter/index.js">10.1 Counter/index.js <a href="#t4610.1 Counter/index.js"> # </a></h3>
<p>src/containers/Counter/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.props.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.increment}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.counter,
    actions
)(Counter);
</code></pre>
<h3 id="t4710.2 src/store/index.js">10.2 src/store/index.js <a href="#t4710.2 src/store/index.js"> # </a></h3>
<p>src/store/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">import</span> {createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> thunk <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-thunk&apos;</span>;
<span class="hljs-keyword">import</span> logger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> createStore(reducers,applyMiddleware(thunk,logger));
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> getStore;
</code></pre>
<h3 id="t4810.3 reducers/index.js">10.3 reducers/index.js <a href="#t4810.3 reducers/index.js"> # </a></h3>
<p>src/store/reducers/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {combineReducers} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./home&apos;</span>;
<span class="hljs-keyword">import</span> counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./counter&apos;</span>;
<span class="hljs-keyword">let</span> reducers=combineReducers({
    home,
    counter
});
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducers;
</code></pre>
<h3 id="t4910.4 reducers/home.js">10.4 reducers/home.js <a href="#t4910.4 reducers/home.js"> # </a></h3>
<p>src/store/reducers/home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState = {
    <span class="hljs-attr">list</span>:[]
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-attr">default</span>:
          <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t5010.5 reducers/counter.js">10.5 reducers/counter.js <a href="#t5010.5 reducers/counter.js"> # </a></h3>
<p>src/store/reducers/counter.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState = {
    <span class="hljs-attr">number</span>: <span class="hljs-number">0</span>
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.INCREMENT:
            <span class="hljs-keyword">return</span> {
                <span class="hljs-attr">number</span>: state.number + <span class="hljs-number">1</span>
            };
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h2 id="t5111. &#x5BA2;&#x6237;&#x7AEF;&#x52A0;&#x8F7D;&#x6570;&#x636E;">11. &#x5BA2;&#x6237;&#x7AEF;&#x52A0;&#x8F7D;&#x6570;&#x636E; <a href="#t5111. &#x5BA2;&#x6237;&#x7AEF;&#x52A0;&#x8F7D;&#x6570;&#x636E;"> # </a></h2>
<h3 id="t5211.1 src/client/index.js">11.1 src/client/index.js <a href="#t5211.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:70}}</span>&gt;</span>
                    {routes}
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t5311.2 Counter/index.js">11.2 Counter/index.js <a href="#t5311.2 Counter/index.js"> # </a></h3>
<p>src/containers/Counter/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/counter&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.props.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.increment}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.counter,
    actions
)(Counter);
</code></pre>
<h3 id="t5411.3 Home/index.js">11.3 Home/index.js <a href="#t5411.3 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/home&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    componentDidMount() {
        <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        {
                            this.props.list.map(item =&gt; (
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            ))
                        }
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(Home);
</code></pre>
<h3 id="t5511.3 Home/index.js">11.3 Home/index.js <a href="#t5511.3 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/home&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    componentDidMount() {
        <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        {
                            this.props.list.map(item =&gt; (
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            ))
                        }
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(Home);
</code></pre>
<h3 id="t5611.4 src/server/render.js">11.4 src/server/render.js <a href="#t5611.4 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">const</span> content=renderToString(
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getStore()}</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:70}}</span>&gt;</span>
                            {routes}
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    );
    res.send(<span class="hljs-string">`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d0ddddc6c1c6c0d3c2f2819c819c85">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
            &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `</span>);
}
</code></pre>
<h3 id="t5711.5 src/store/action-types.js">11.5 src/store/action-types.js <a href="#t5711.5 src/store/action-types.js"> # </a></h3>
<p>src/store/action-types.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//counter</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT=<span class="hljs-string">&apos;INCREMENT&apos;</span>;

<span class="hljs-comment">//home</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SET_HOME_LIST=<span class="hljs-string">&apos;SET_HOME_LIST&apos;</span>;

</code></pre>
<h3 id="t5811.6 reducers/home.js">11.6 reducers/home.js <a href="#t5811.6 reducers/home.js"> # </a></h3>
<p>src/store/reducers/home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState = {
    <span class="hljs-attr">list</span>:[]
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.SET_HOME_LIST:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">list</span>:action.payload};
        <span class="hljs-keyword">default</span>:
          <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t5911.7  actions/counter.js">11.7  actions/counter.js <a href="#t5911.7  actions/counter.js"> # </a></h3>
<p>src/store/actions/counter.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    increment() {
        <span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:types.INCREMENT}
    }
}
</code></pre>
<h3 id="t6011.8 reducers/home.js">11.8 reducers/home.js <a href="#t6011.8 reducers/home.js"> # </a></h3>
<p>src/store/reducers/home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    getHomeList() {
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState</span>) </span>{
            axios.get(<span class="hljs-string">&apos;http://localhost:4000/api/users&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                <span class="hljs-keyword">let</span> list=result.data;
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_HOME_LIST,
                    <span class="hljs-attr">payload</span>:list
                });
            });
        }
    }
}
</code></pre>
<h3 id="t6111.9 api/server.js">11.9 api/server.js <a href="#t6111.9 api/server.js"> # </a></h3>
<p>api/server.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> cors=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cors&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
<span class="hljs-keyword">var</span> corsOptions = {
    <span class="hljs-attr">origin</span>: <span class="hljs-string">&apos;http://localhost:9090&apos;</span>,
    <span class="hljs-attr">optionsSuccessStatus</span>: <span class="hljs-number">200</span> 
}
app.use(cors(corsOptions));
<span class="hljs-keyword">let</span> users=[{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}];
app.get(<span class="hljs-string">&apos;/api/users&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.json(users);
});
app.listen(<span class="hljs-number">4000</span>);
</code></pre>
<h2 id="t6212. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8DEF;&#x7531;">12. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8DEF;&#x7531; <a href="#t6212. &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t6312.1 src/client/index.js">12.1 src/client/index.js <a href="#t6312.1 src/client/index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> {Route} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:</span> <span class="hljs-attr">70</span>}}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {routes.map(route =&gt; (
                            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> {<span class="hljs-attr">...route</span>}/&gt;</span>
                      ))}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>

                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span>
    ,document.querySelector(&apos;#root&apos;));
</span></code></pre>
<h3 id="t6412.2 Home/index.js">12.2 Home/index.js <a href="#t6412.2 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/home&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x52A0;&#x8F7D;&#x6570;&#x636E;&apos;</span>);
    }
    <span class="hljs-comment">//componentDidMount&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x662F;&#x4E0D;&#x6267;&#x884C;&#x7684;</span>
    componentDidMount() {
        <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        {
                            this.props.list.map(item =&gt; (
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            ))
                        }
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(Home);
</code></pre>
<h3 id="t6512.3 src/routes.js">12.3 src/routes.js <a href="#t6512.3 src/routes.js"> # </a></h3>
<p>src/routes.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Route} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
        <span class="hljs-attr">component</span>: Home,
        <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;home&apos;</span>,
        <span class="hljs-attr">loadData</span>:Home.loadData
    },
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/counter&apos;</span>,
        <span class="hljs-attr">component</span>: Counter,
        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;login&apos;</span>,
        <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
    }
]
<span class="hljs-comment">/**
export default (
    &lt;Fragment&gt;
        &lt;Route path=&quot;/&quot; exact component={Home}&gt;&lt;/Route&gt;
        &lt;Route path=&quot;/counter&quot; exact component={Counter}&gt;&lt;/Route&gt;
    &lt;/Fragment&gt;
)
*/</span>
</code></pre>
<h3 id="t6612.4 src/server/render.js">12.4 src/server/render.js <a href="#t6612.4 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {Route,matchPath} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore();
    <span class="hljs-keyword">let</span> matchedRoutes=routes.filter(<span class="hljs-function"><span class="hljs-params">route</span> =&gt;</span> {
        <span class="hljs-keyword">return</span> matchPath(req.path,route);
    });
    <span class="hljs-built_in">console</span>.log(matchedRoutes);
    <span class="hljs-keyword">const</span> content=renderToString(
        &lt;Provider store={store}&gt;
            &lt;StaticRouter context={{}} location={req.path}&gt;
                    &lt;Fragment&gt;
                        &lt;Header/&gt;
                        &lt;div className=&quot;container&quot; style={{marginTop:70}}&gt;
                        {routes.map(route =&gt; (
                            &lt;Route {...route}/&gt;
                            ))}
                        &lt;/div&gt;
                    &lt;/Fragment&gt;
                &lt;/StaticRouter&gt;
        &lt;/Provider&gt;
    );
    res.send(`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680a07071c1b1c1a0918285b465b465f">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
            &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;${content}&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `);
}
</code></pre>
<h2 id="t6713.&#x591A;&#x7EA7;&#x8DEF;&#x7531;">13.&#x591A;&#x7EA7;&#x8DEF;&#x7531; <a href="#t6713.&#x591A;&#x7EA7;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t6813.1 src/client/index.js">13.1 src/client/index.js <a href="#t6813.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> {Route,matchPath} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:</span> <span class="hljs-attr">70</span>}}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>

                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t6913.2 Header/index.js">13.2 Header/index.js <a href="#t6913.2 Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-inverse navbar-fixed-top&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;SSR<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;navbar&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;collapse navbar-collapse&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/counter&quot;</span>&gt;</span>Counter<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t7013.3 src/routes.js">13.3 src/routes.js <a href="#t7013.3 src/routes.js"> # </a></h3>
<p>src/routes.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User&apos;</span>;
<span class="hljs-keyword">import</span> UserList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User/components/UserList&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
        <span class="hljs-attr">component</span>: Home,
        <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/home&apos;</span>,
        <span class="hljs-attr">loadData</span>:Home.loadData
    },
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user&apos;</span>,
        <span class="hljs-attr">component</span>: User,
        <span class="hljs-attr">key</span>: <span class="hljs-string">&apos;/user&apos;</span>,
        <span class="hljs-attr">routes</span>: [
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user/list&apos;</span>,
                <span class="hljs-attr">component</span>: UserList,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/user/list&apos;</span>
            }
        ]
    },
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/counter&apos;</span>,
        <span class="hljs-attr">component</span>: Counter,
        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;login&apos;</span>,
        <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
    }
]
<span class="hljs-comment">/**
export default (
    &lt;Fragment&gt;
        &lt;Route path=&quot;/&quot; exact component={Home}&gt;&lt;/Route&gt;
        &lt;Route path=&quot;/counter&quot; exact component={Counter}&gt;&lt;/Route&gt;
    &lt;/Fragment&gt;
)
*/</span>
</code></pre>
<h3 id="t7113.4 src/server/render.js">13.4 src/server/render.js <a href="#t7113.4 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {Route,matchPath} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> getStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore();
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes= matchRoutes(routes,req.path);
    <span class="hljs-built_in">console</span>.log(matchedRoutes);
    <span class="hljs-keyword">const</span> content=renderToString(
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:70}}</span>&gt;</span>
                          {renderRoutes(routes)}
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    );
    res.send(<span class="hljs-string">`
    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
        &lt;head&gt;
            &lt;meta charset=&quot;UTF-8&quot;&gt;
            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
            &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
            &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a1acacb7b0b7b1a2b383f0edf0edf4">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
            &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
            &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
        &lt;/body&gt;
    &lt;/html&gt;
    `</span>);
}
</code></pre>
<h3 id="t7213.5 src/server/render.js">13.5 src/server/render.js <a href="#t7213.5 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.props.children);
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-3&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/add&quot;</span>&gt;</span>&#x6DFB;&#x52A0;&#x7528;&#x6237;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-9&quot;</span>&gt;</span>
                    {renderRoutes(this.props.route.routes)}
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t7313.6 components/UserList.js">13.6 components/UserList.js <a href="#t7313.6 components/UserList.js"> # </a></h3>
<p>src/containers/User/components/UserList.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../../store/actions/home&apos;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x52A0;&#x8F7D;&#x6570;&#x636E;&apos;</span>);
    }
    <span class="hljs-comment">//componentDidMount&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x662F;&#x4E0D;&#x6267;&#x884C;&#x7684;</span>
    componentDidMount() {
        <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                {
                    this.props.list.map(item =&gt; (
                        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                    ))
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(UserList);
</code></pre>
<h2 id="t7414.&#x540E;&#x53F0;&#x83B7;&#x53D6;&#x6570;&#x636E;">14.&#x540E;&#x53F0;&#x83B7;&#x53D6;&#x6570;&#x636E; <a href="#t7414.&#x540E;&#x53F0;&#x83B7;&#x53D6;&#x6570;&#x636E;"> # </a></h2>
<h3 id="t7514.1 src/client/index.js">14.1 src/client/index.js <a href="#t7514.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getClientStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getClientStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:</span> <span class="hljs-attr">70</span>}}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>

                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t7614.2 Home/index.js">14.2 Home/index.js <a href="#t7614.2 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/home&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function">(<span class="hljs-params">store</span>) =&gt;</span> {
        <span class="hljs-comment">//dispatch&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;action</span>
        <span class="hljs-comment">//https://github.com/reduxjs/redux/blob/master/src/createStore.js</span>
        <span class="hljs-keyword">return</span> store.dispatch(actions.getHomeList());
    }
    <span class="hljs-comment">//componentDidMount&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x662F;&#x4E0D;&#x6267;&#x884C;&#x7684;</span>
    componentDidMount() {
        <span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.props.list.length==<span class="hljs-number">0</span>)
            <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        {
                            this.props.list.map(item =&gt; (
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            ))
                        }
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(Home);
</code></pre>
<h3 id="t7714.3 src/server/render.js">14.3 src/server/render.js <a href="#t7714.3 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {Route,matchPath} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore();
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes=matchRoutes(routes,req.path);
    <span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData)
            promises.push(item.route.loadData(store));
    });
    <span class="hljs-built_in">Promise</span>.all(promises).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> content=renderToString(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:70}}</span>&gt;</span>
                              {renderRoutes(routes)}
                            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        );
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1c3ceced5d2d5d3c0d1e1928f928f96">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
    });

}
</code></pre>
<h3 id="t7814.4 actions/home.js">14.4 actions/home.js <a href="#t7814.4 actions/home.js"> # </a></h3>
<p>src/store/actions/home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    getHomeList() {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState</span>) </span>{
            <span class="hljs-keyword">return</span> axios.get(<span class="hljs-string">&apos;http://localhost:4000/api/users&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                <span class="hljs-keyword">let</span> list=result.data;
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_HOME_LIST,
                    <span class="hljs-attr">payload</span>:list
                });
            });
        }
    }
}
</code></pre>
<h3 id="t7914.5 src/store/index.js">14.5 src/store/index.js <a href="#t7914.5 src/store/index.js"> # </a></h3>
<p>src/store/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">import</span> {createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> thunk <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-thunk&apos;</span>;
<span class="hljs-keyword">import</span> logger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> createStore(reducers,applyMiddleware(thunk,logger));
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClientStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> initState=<span class="hljs-built_in">window</span>.context.state;
    <span class="hljs-keyword">return</span> createStore(reducers,initState,applyMiddleware(thunk,logger));
}
</code></pre>
<h2 id="t8015. Node&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;">15. Node&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668; <a href="#t8015. Node&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;"> # </a></h2>
<h3 id="t8115.1 src/server/index.js">15.1 src/server/index.js <a href="#t8115.1 src/server/index.js"> # </a></h3>
<p>src/server/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">import</span> proxy <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express-http-proxy&apos;</span>;
<span class="hljs-keyword">import</span> render <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./render&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>));
app.use(<span class="hljs-string">&apos;/api&apos;</span>,proxy(<span class="hljs-string">&apos;http://127.0.0.1:4000&apos;</span>,{
    <span class="hljs-comment">//&#x4FEE;&#x6539;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;</span>
    proxyReqPathResolver: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req</span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">`/api/<span class="hljs-subst">${req.url}</span>`</span>;
    }
}));
<span class="hljs-comment">//context&#x6570;&#x636E;&#x7684;&#x4F20;&#x9012; StaticRouter&#x9700;&#x8981;&#x77E5;&#x9053;&#x5F53;&#x524D;&#x8DEF;&#x5F84;</span>
app.get(<span class="hljs-string">&apos;*&apos;</span>,(req,res) =&gt; {
    render(req,res);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t8215.2 src/server/render.js">15.2 src/server/render.js <a href="#t8215.2 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore();
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes=matchRoutes(routes,req.path);
    <span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData)
            promises.push(item.route.loadData(store));
    });
    <span class="hljs-built_in">Promise</span>.all(promises).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> content=renderToString(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:70}}</span>&gt;</span>
                              {renderRoutes(routes)}
                            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        );
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffdf0f0ebecebedfeefdfacb1acb1a8">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
    });
}
</code></pre>
<h3 id="t8315.3 actions/home.js">15.3 actions/home.js <a href="#t8315.3 actions/home.js"> # </a></h3>
<p>src/store/actions/home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    getHomeList() {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.get(<span class="hljs-string">&apos;/api/users&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                <span class="hljs-keyword">let</span> list=result.data;
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_HOME_LIST,
                    <span class="hljs-attr">payload</span>:list
                });
            });
        }
    }
}
</code></pre>
<h3 id="t8415.4 src/store/index.js">15.4 src/store/index.js <a href="#t8415.4 src/store/index.js"> # </a></h3>
<p>src/store/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">import</span> {createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> clientRequest <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../client/request&apos;</span>;
<span class="hljs-keyword">import</span> serverRequest <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../server/request&apos;</span>;
<span class="hljs-keyword">import</span> thunk <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-thunk&apos;</span>;
<span class="hljs-keyword">import</span> logger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> createStore(reducers,applyMiddleware(thunk.withExtraArgument(serverRequest),logger));
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClientStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> initState=<span class="hljs-built_in">window</span>.context.state;
    <span class="hljs-keyword">return</span> createStore(reducers,initState,applyMiddleware(thunk.withExtraArgument(clientRequest),logger));
}
</code></pre>
<h3 id="t8515.5 src/client/request.js">15.5 src/client/request.js <a href="#t8515.5 src/client/request.js"> # </a></h3>
<p>src/client/request.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> axios.create({
    <span class="hljs-attr">baseURL</span>:<span class="hljs-string">&apos;/&apos;</span>
});
</code></pre>
<h3 id="t8615.6 src/server/request.js">15.6 src/server/request.js <a href="#t8615.6 src/server/request.js"> # </a></h3>
<p>src/server/request.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> axios.create({
    <span class="hljs-attr">baseURL</span>:<span class="hljs-string">&apos;http://localhost:4000/&apos;</span>
});
</code></pre>
<h2 id="t8716. &#x62BD;&#x53D6;App.js">16. &#x62BD;&#x53D6;App.js <a href="#t8716. &#x62BD;&#x53D6;App.js"> # </a></h2>
<h3 id="t8816.1 src/client/index.js">16.1 src/client/index.js <a href="#t8816.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getClientStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getClientStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
          {renderRoutes(routes)}
      <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t8916.2 User/index.js">16.2 User/index.js <a href="#t8916.2 User/index.js"> # </a></h3>
<p>src/containers/User/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-3&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/add&quot;</span>&gt;</span>&#x6DFB;&#x52A0;&#x7528;&#x6237;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-9&quot;</span>&gt;</span>
                    {renderRoutes(this.props.route.routes)}
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t9016.3 src/routes.js">16.3 src/routes.js <a href="#t9016.3 src/routes.js"> # </a></h3>
<p>src/routes.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User&apos;</span>;
<span class="hljs-keyword">import</span> UserList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User/components/UserList&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
        <span class="hljs-attr">component</span>: App,
        <span class="hljs-attr">routes</span>: [
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
                <span class="hljs-attr">component</span>: Home,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/home&apos;</span>,
                <span class="hljs-attr">loadData</span>:Home.loadData
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">component</span>: User,
                <span class="hljs-attr">key</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">routes</span>: [
                    {
                        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user/list&apos;</span>,
                        <span class="hljs-attr">component</span>: UserList,
                        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/user/list&apos;</span>
                    }
                ]
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/counter&apos;</span>,
                <span class="hljs-attr">component</span>: Counter,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;login&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            }
        ]
    }

]
<span class="hljs-comment">/**
export default (
    &lt;Fragment&gt;
        &lt;Route path=&quot;/&quot; exact component={Home}&gt;&lt;/Route&gt;
        &lt;Route path=&quot;/counter&quot; exact component={Counter}&gt;&lt;/Route&gt;
    &lt;/Fragment&gt;
)
*/</span>
</code></pre>
<h3 id="t9116.4 src/server/index.js">16.4 src/server/index.js <a href="#t9116.4 src/server/index.js"> # </a></h3>
<p>src/server/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">import</span> proxy <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express-http-proxy&apos;</span>;
<span class="hljs-keyword">import</span> render <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./render&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>));
app.use(<span class="hljs-string">&apos;/api&apos;</span>,proxy(<span class="hljs-string">&apos;http://localhost:4000&apos;</span>,{
    <span class="hljs-comment">//&#x4FEE;&#x6539;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;</span>
    proxyReqPathResolver: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req</span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">`/api<span class="hljs-subst">${req.url}</span>`</span>;
    }
}));
<span class="hljs-comment">//context&#x6570;&#x636E;&#x7684;&#x4F20;&#x9012; StaticRouter&#x9700;&#x8981;&#x77E5;&#x9053;&#x5F53;&#x524D;&#x8DEF;&#x5F84;</span>
app.get(<span class="hljs-string">&apos;*&apos;</span>,(req,res) =&gt; {
    render(req,res);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t9216.5 src/server/render.js">16.5 src/server/render.js <a href="#t9216.5 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore();
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes=matchRoutes(routes,req.path);
    <span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData)
            promises.push(item.route.loadData(store));
    });
    <span class="hljs-built_in">Promise</span>.all(promises).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> content=renderToString(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    {renderRoutes(routes)}
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        );
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7858888939493958697a7d4c9d4c9d0">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
    });
}
</code></pre>
<h3 id="t9316.6  src/containers/App.js">16.6  src/containers/App.js <a href="#t9316.6  src/containers/App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:</span> <span class="hljs-attr">70</span>}}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(this.props.route.routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h2 id="t9417. &#x5B9E;&#x73B0;&#x5934;&#x90E8;&#x5BFC;&#x822A;">17. &#x5B9E;&#x73B0;&#x5934;&#x90E8;&#x5BFC;&#x822A; <a href="#t9417. &#x5B9E;&#x73B0;&#x5934;&#x90E8;&#x5BFC;&#x822A;"> # </a></h2>
<h3 id="t9517.1 src/client/index.js">17.1 src/client/index.js <a href="#t9517.1 src/client/index.js"> # </a></h3>
<p>src/client/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getClientStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
ReactDOM.hydrate(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{getClientStore()}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
          {renderRoutes(routes)}
      <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
    ,<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#root&apos;</span>));
</code></pre>
<h3 id="t9617.2 src/components/Header/index.js">17.2 src/components/Header/index.js <a href="#t9617.2 src/components/Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Header</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-inverse navbar-fixed-top&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;SSR<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;navbar&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;collapse navbar-collapse&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              {
                                !this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/login&quot;</span>&gt;</span>&#x767B;&#x5F55;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              }
                              {
                                this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/logout&quot;</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/profile&quot;</span>&gt;</span>&#x4E2A;&#x4EBA;&#x4E2D;&#x5FC3;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                              }
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>state.session
)(Header);
</code></pre>
<h3 id="t9717.3 src/store/action-types.js">17.3 src/store/action-types.js <a href="#t9717.3 src/store/action-types.js"> # </a></h3>
<p>src/store/action-types.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//counter</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT=<span class="hljs-string">&apos;INCREMENT&apos;</span>;
<span class="hljs-comment">//home</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SET_HOME_LIST=<span class="hljs-string">&apos;SET_HOME_LIST&apos;</span>;
<span class="hljs-comment">//&#x767B;&#x5F55;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> LOGIN=<span class="hljs-string">&apos;LOGIN&apos;</span>;
<span class="hljs-comment">//&#x9000;&#x51FA;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> LOGOUT=<span class="hljs-string">&apos;LOGOUT&apos;</span>;
</code></pre>
<h3 id="t9817.4 reducers/index.js">17.4 reducers/index.js <a href="#t9817.4 reducers/index.js"> # </a></h3>
<p>src/store/reducers/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {combineReducers} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./home&apos;</span>;
<span class="hljs-keyword">import</span> counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./counter&apos;</span>;
<span class="hljs-keyword">import</span> session <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./session&apos;</span>;
<span class="hljs-keyword">let</span> reducers=combineReducers({
    home,
    counter,
    session
});
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducers;
</code></pre>
<h3 id="t9917.5  reducers/session.js">17.5  reducers/session.js <a href="#t9917.5  reducers/session.js"> # </a></h3>
<p>src/store/reducers/session.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState = {
    <span class="hljs-attr">user</span>:<span class="hljs-literal">null</span>
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-attr">default</span>:
          <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h2 id="t10018. &#x5B9E;&#x73B0;&#x767B;&#x5F55;&#x529F;&#x80FD;">18. &#x5B9E;&#x73B0;&#x767B;&#x5F55;&#x529F;&#x80FD; <a href="#t10018. &#x5B9E;&#x73B0;&#x767B;&#x5F55;&#x529F;&#x80FD;"> # </a></h2>
<h3 id="t10118.1 api/server.js">18.1 api/server.js <a href="#t10118.1 api/server.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> cors=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cors&apos;</span>);
<span class="hljs-keyword">let</span> morgan=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;morgan&apos;</span>);
<span class="hljs-keyword">let</span> session=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express-session&apos;</span>);
<span class="hljs-keyword">let</span> bodyParser=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;body-parser&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
app.use(morgan(<span class="hljs-string">&apos;tiny&apos;</span>));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({<span class="hljs-attr">extended</span>: <span class="hljs-literal">true</span>}));
app.use(session({
    <span class="hljs-attr">resave</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">secret</span>:<span class="hljs-string">&apos;zfpx&apos;</span>
}));
<span class="hljs-keyword">var</span> corsOptions = {
    <span class="hljs-attr">origin</span>: <span class="hljs-string">&apos;*&apos;</span>,
    <span class="hljs-attr">optionsSuccessStatus</span>: <span class="hljs-number">200</span> 
}
app.use(cors(corsOptions));
<span class="hljs-keyword">let</span> users=[{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}];
app.get(<span class="hljs-string">&apos;/api/users&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.json(users);
});
app.post(<span class="hljs-string">&apos;/api/login&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> user=req.body;
    req.session.user=user;
    res.json({
        <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
        user,
        <span class="hljs-attr">success</span>:<span class="hljs-string">&apos;&#x767B;&#x5F55;&#x6210;&#x529F;&apos;</span>
    });
});
app.get(<span class="hljs-string">&apos;/api/logout&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    req.session.user=<span class="hljs-literal">null</span>;
    res.json({
        <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
        <span class="hljs-attr">success</span>:<span class="hljs-string">&apos;&#x9000;&#x51FA;&#x6210;&#x529F;&apos;</span>
    });
});
app.get(<span class="hljs-string">&apos;/api/user&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">if</span> (req.session.user) {
        res.json({
            <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
            user,
            <span class="hljs-attr">success</span>:<span class="hljs-string">&apos;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x6210;&#x529F;&apos;</span>
        });
    } <span class="hljs-keyword">else</span> {
        res.json({
            <span class="hljs-attr">code</span>: <span class="hljs-number">1</span>,
            <span class="hljs-attr">error</span>:<span class="hljs-string">&apos;&#x6B64;&#x7528;&#x6237;&#x672A;&#x767B;&#x5F55;&apos;</span>
        });
    }
});
app.listen(<span class="hljs-number">4000</span>);
</code></pre>
<h3 id="t10218.2 Header/index.js">18.2 Header/index.js <a href="#t10218.2 Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Header</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-inverse navbar-fixed-top&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;SSR<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;navbar&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;collapse navbar-collapse&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              {
                                !this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/login&quot;</span>&gt;</span>&#x767B;&#x5F55;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              }
                              {
                                this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/logout&quot;</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/profile&quot;</span>&gt;</span>&#x4E2A;&#x4EBA;&#x4E2D;&#x5FC3;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                              }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        {
                            this.props.user&amp;&amp;(
                                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;nav navbar-nav navbar-right&quot;</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x6B22;&#x8FCE; {this.props.user.username}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                            )
                         }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>state.session
)(Header);
</code></pre>
<h3 id="t10318.3 src/containers/App.js">18.3 src/containers/App.js <a href="#t10318.3 src/containers/App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function">(<span class="hljs-params">store</span>) =&gt;</span> {
        store.dispatch();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:</span> <span class="hljs-attr">70</span>}}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(this.props.route.routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t10418.4 src/routes.js">18.4 src/routes.js <a href="#t10418.4 src/routes.js"> # </a></h3>
<p>src/routes.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User&apos;</span>;
<span class="hljs-keyword">import</span> UserList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User/components/UserList&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">import</span> Login <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Login&apos;</span>;
<span class="hljs-keyword">import</span> Logout <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Logout&apos;</span>;
<span class="hljs-keyword">import</span> Profile <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Profile&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
        <span class="hljs-attr">component</span>: App,
        <span class="hljs-attr">routes</span>: [
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
                <span class="hljs-attr">component</span>: Home,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/home&apos;</span>,
                <span class="hljs-attr">loadData</span>:Home.loadData
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">component</span>: User,
                <span class="hljs-attr">key</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">routes</span>: [
                    {
                        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user/list&apos;</span>,
                        <span class="hljs-attr">component</span>: UserList,
                        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/user/list&apos;</span>
                    }
                ]
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/counter&apos;</span>,
                <span class="hljs-attr">component</span>: Counter,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;counter&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/login&apos;</span>,
                <span class="hljs-attr">component</span>: Login,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/login&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/logout&apos;</span>,
                <span class="hljs-attr">component</span>: Logout,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/logout&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/profile&apos;</span>,
                <span class="hljs-attr">component</span>: Profile,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/profile&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            }
        ]
    }

]
<span class="hljs-comment">/**
export default (
    &lt;Fragment&gt;
        &lt;Route path=&quot;/&quot; exact component={Home}&gt;&lt;/Route&gt;
        &lt;Route path=&quot;/counter&quot; exact component={Counter}&gt;&lt;/Route&gt;
    &lt;/Fragment&gt;
)
*/</span>
</code></pre>
<h3 id="t10518.5 src/server/index.js">18.5 src/server/index.js <a href="#t10518.5 src/server/index.js"> # </a></h3>
<p>src/server/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express&apos;</span>;
<span class="hljs-keyword">import</span> proxy <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;express-http-proxy&apos;</span>;
<span class="hljs-keyword">import</span> render <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./render&apos;</span>;
<span class="hljs-keyword">let</span> app=express();
app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>));

app.use(<span class="hljs-string">&apos;/api&apos;</span>,proxy(<span class="hljs-string">&apos;http://localhost:4000&apos;</span>,{
    <span class="hljs-comment">//&#x4FEE;&#x6539;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;</span>
    proxyReqPathResolver: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req</span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">`/api<span class="hljs-subst">${req.url}</span>`</span>;
    }
}));
<span class="hljs-comment">//context&#x6570;&#x636E;&#x7684;&#x4F20;&#x9012; StaticRouter&#x9700;&#x8981;&#x77E5;&#x9053;&#x5F53;&#x524D;&#x8DEF;&#x5F84;</span>
app.get(<span class="hljs-string">&apos;*&apos;</span>,(req,res) =&gt; {
    render(req,res);
});
app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h3 id="t10618.6 action-types.js">18.6 action-types.js <a href="#t10618.6 action-types.js"> # </a></h3>
<p>src/store/action-types.js</p>
<pre><code class="lang-js"><span class="hljs-comment">//counter</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT=<span class="hljs-string">&apos;INCREMENT&apos;</span>;
<span class="hljs-comment">//home</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SET_HOME_LIST=<span class="hljs-string">&apos;SET_HOME_LIST&apos;</span>;
<span class="hljs-comment">//&#x767B;&#x5F55;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> LOGIN=<span class="hljs-string">&apos;LOGIN&apos;</span>;
<span class="hljs-comment">//&#x9000;&#x51FA;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> LOGOUT=<span class="hljs-string">&apos;LOGOUT&apos;</span>;
<span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x4F1A;&#x8BDD;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SET_SESSION=<span class="hljs-string">&apos;SET_SESSION&apos;</span>;

</code></pre>
<h3 id="t10718.7 reducers/session.js">18.7 reducers/session.js <a href="#t10718.7 reducers/session.js"> # </a></h3>
<p>src/store/reducers/session.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState = {
    <span class="hljs-attr">user</span>: <span class="hljs-literal">null</span>,
    <span class="hljs-attr">success</span>: <span class="hljs-literal">null</span>,
    <span class="hljs-attr">error</span>:<span class="hljs-literal">null</span>
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.SET_SESSION:
            <span class="hljs-keyword">return</span> {...action.payload};
        <span class="hljs-keyword">default</span>:
          <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t10818.7 Login/index.js">18.7 Login/index.js <a href="#t10818.7 Login/index.js"> # </a></h3>
<p>src/containers/Login/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/session&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Login</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    state={
        <span class="hljs-attr">username</span>:<span class="hljs-string">&apos;&apos;</span>
    }
    handleChange=<span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
        <span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">username</span>:event.target.value});    
    }
    handleSubmit=<span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
        event.preventDefault();
        <span class="hljs-keyword">this</span>.props.login(<span class="hljs-keyword">this</span>.state);
    }
    render() {
        <span class="hljs-keyword">return</span> (
            &lt;div className=&quot;row&quot;&gt;
                &lt;div className=&quot;col-md-12&quot;&gt;
                    &lt;form onSubmit={this.handleSubmit}&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;label htmlFor=&quot;username&quot;&gt;&#x7528;&#x6237;&#x540D;&lt;/label&gt;
                            &lt;input value={this.state.username} onChange={this.handleChange} type=&quot;text&quot; className=&quot;form-control&quot;/&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;input type=&quot;submit&quot; className=&quot;btn btn-primary&quot;/&gt;
                        &lt;/div&gt;
                    &lt;/form&gt;

                &lt;/div&gt;
            &lt;/div&gt;
        )
    }
}
export default connect(
    state =&gt; state.session,
    actions
)(Login)
</code></pre>
<h3 id="t10918.8 Logout/index.js">18.8 Logout/index.js <a href="#t10918.8 Logout/index.js"> # </a></h3>
<p>src/containers/Logout/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Profile</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t11018.9 Profile/index.js">18.9 Profile/index.js <a href="#t11018.9 Profile/index.js"> # </a></h3>
<p>src/containers/Profile/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Profile</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    &#x4E2A;&#x4EBA;&#x4E2D;&#x5FC3;
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t11118.10 actions/session.js">18.10 actions/session.js <a href="#t11118.10 actions/session.js"> # </a></h3>
<p>src/store/actions/session.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    login(user) {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.post(<span class="hljs-string">&apos;/api/login&apos;</span>,user).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_SESSION,
                    <span class="hljs-attr">payload</span>:result.data
                });
            });
        }
    }
}
</code></pre>
<h2 id="t11219. &#x9000;&#x51FA;&#x529F;&#x80FD;">19. &#x9000;&#x51FA;&#x529F;&#x80FD; <a href="#t11219. &#x9000;&#x51FA;&#x529F;&#x80FD;"> # </a></h2>
<h3 id="t11319.1 Logout/index.js">19.1 Logout/index.js <a href="#t11319.1 Logout/index.js"> # </a></h3>
<p>src/containers/Logout/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/session&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Profile</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    handleLogout=<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">this</span>.props.logout();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleLogout}</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.session,
    actions
)(Profile)
</code></pre>
<h3 id="t11419.2 actions/session.js">19.2 actions/session.js <a href="#t11419.2 actions/session.js"> # </a></h3>
<p>src/store/actions/session.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    login(user) {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.post(<span class="hljs-string">&apos;/api/login&apos;</span>,user).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_SESSION,
                    <span class="hljs-attr">payload</span>:result.data
                });
            });
        }
    },
    logout() {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.get(<span class="hljs-string">&apos;/api/logout&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_SESSION,
                    <span class="hljs-attr">payload</span>:result.data
                });
            });
        }
    }
}
</code></pre>
<h2 id="t11520. &#x52A0;&#x8F7D;&#x7528;&#x6237;&#x4FE1;&#x606F;">20. &#x52A0;&#x8F7D;&#x7528;&#x6237;&#x4FE1;&#x606F; <a href="#t11520. &#x52A0;&#x8F7D;&#x7528;&#x6237;&#x4FE1;&#x606F;"> # </a></h2>
<h3 id="t11620.1 api/server.js">20.1 api/server.js <a href="#t11620.1 api/server.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> cors=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cors&apos;</span>);
<span class="hljs-keyword">let</span> morgan=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;morgan&apos;</span>);
<span class="hljs-keyword">let</span> session=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express-session&apos;</span>);
<span class="hljs-keyword">let</span> bodyParser=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;body-parser&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
app.use(morgan(<span class="hljs-string">&apos;tiny&apos;</span>));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({<span class="hljs-attr">extended</span>: <span class="hljs-literal">true</span>}));
app.use(session({
    <span class="hljs-attr">resave</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">secret</span>:<span class="hljs-string">&apos;zfpx&apos;</span>
}));
<span class="hljs-keyword">var</span> corsOptions = {
    <span class="hljs-attr">origin</span>: <span class="hljs-string">&apos;*&apos;</span>,
    <span class="hljs-attr">optionsSuccessStatus</span>: <span class="hljs-number">200</span> 
}
app.use(cors(corsOptions));
<span class="hljs-keyword">let</span> users=[{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}];
app.get(<span class="hljs-string">&apos;/api/users&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.json(users);
});
app.post(<span class="hljs-string">&apos;/api/login&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> user=req.body;
    req.session.user=user;
    res.json({
        <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
        <span class="hljs-attr">data</span>: {
            user,
            <span class="hljs-attr">success</span>:<span class="hljs-string">&apos;&#x767B;&#x5F55;&#x6210;&#x529F;&apos;</span>
        }
    });
});
app.get(<span class="hljs-string">&apos;/api/logout&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    req.session.user=<span class="hljs-literal">null</span>;
    res.json({
        <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
        <span class="hljs-attr">data</span>: {
            <span class="hljs-attr">success</span>:<span class="hljs-string">&apos;&#x9000;&#x51FA;&#x6210;&#x529F;&apos;</span>
        }

    });
});
app.get(<span class="hljs-string">&apos;/api/user&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">if</span> (req.session.user) {
        res.json({
            <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
            <span class="hljs-attr">data</span>: {
                <span class="hljs-attr">user</span>:req.session.user,
                <span class="hljs-attr">success</span>:<span class="hljs-string">&apos;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x6210;&#x529F;&apos;</span>
            }
        });
    } <span class="hljs-keyword">else</span> {
        res.json({
            <span class="hljs-attr">code</span>: <span class="hljs-number">1</span>,
            <span class="hljs-attr">data</span>: {
                <span class="hljs-attr">error</span>:<span class="hljs-string">&apos;&#x6B64;&#x7528;&#x6237;&#x672A;&#x767B;&#x5F55;&apos;</span>
            }
        });
    }

});
app.listen(<span class="hljs-number">4000</span>);
</code></pre>
<h3 id="t11720.2 containers/App.js">20.2 containers/App.js <a href="#t11720.2 containers/App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions/session&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function">(<span class="hljs-params">store</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> store.dispatch(actions.getUser());
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{marginTop:</span> <span class="hljs-attr">70</span>}}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(this.props.route.routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t11820.3 Profile/index.js">20.3 Profile/index.js <a href="#t11820.3 Profile/index.js"> # </a></h3>
<p>src/containers/Profile/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/session&apos;</span>;
<span class="hljs-keyword">import</span> {Redirect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Profile</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.props.user? (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    {this.props.user.username}
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        ):<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Redirect</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/login&quot;</span>/&gt;</span>;
    }
}
export default connect(
    state =&gt; state.session,
    actions
)(Profile)
</span></code></pre>
<h3 id="t11920.4 src/routes.js">20.4 src/routes.js <a href="#t11920.4 src/routes.js"> # </a></h3>
<p>src/routes.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User&apos;</span>;
<span class="hljs-keyword">import</span> UserList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User/components/UserList&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">import</span> Login <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Login&apos;</span>;
<span class="hljs-keyword">import</span> Logout <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Logout&apos;</span>;
<span class="hljs-keyword">import</span> Profile <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Profile&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
        <span class="hljs-attr">component</span>: App,
        <span class="hljs-attr">loadData</span>:App.loadData,
        <span class="hljs-attr">routes</span>: [
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
                <span class="hljs-attr">component</span>: Home,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/home&apos;</span>,
                <span class="hljs-attr">loadData</span>:Home.loadData
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">component</span>: User,
                <span class="hljs-attr">key</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">routes</span>: [
                    {
                        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user/list&apos;</span>,
                        <span class="hljs-attr">component</span>: UserList,
                        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/user/list&apos;</span>
                    }
                ]
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/counter&apos;</span>,
                <span class="hljs-attr">component</span>: Counter,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;counter&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/login&apos;</span>,
                <span class="hljs-attr">component</span>: Login,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/login&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/logout&apos;</span>,
                <span class="hljs-attr">component</span>: Logout,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/logout&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/profile&apos;</span>,
                <span class="hljs-attr">component</span>: Profile,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/profile&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            }
        ]
    }

]
<span class="hljs-comment">/**
export default (
    &lt;Fragment&gt;
        &lt;Route path=&quot;/&quot; exact component={Home}&gt;&lt;/Route&gt;
        &lt;Route path=&quot;/counter&quot; exact component={Counter}&gt;&lt;/Route&gt;
    &lt;/Fragment&gt;
)
*/</span>
</code></pre>
<h3 id="t12020.5 src/server/render.js">20.5 src/server/render.js <a href="#t12020.5 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore(req);
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes=matchRoutes(routes,req.path);
    <span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData)
            promises.push(item.route.loadData(store));
    });
    <span class="hljs-built_in">Promise</span>.all(promises).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> content=renderToString(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{{}}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    {renderRoutes(routes)}
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        );
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7974746f686f697a6b5b283528352c">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
    });

}
</code></pre>
<h3 id="t12120.6 src/server/request.js">20.6 src/server/request.js <a href="#t12120.6 src/server/request.js"> # </a></h3>
<p>src/server/request.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> (req)=&gt;axios.create({
    <span class="hljs-attr">baseURL</span>: <span class="hljs-string">&apos;http://localhost:4000/&apos;</span>,
    <span class="hljs-attr">headers</span>: {
        <span class="hljs-attr">cookie</span>:req.get(<span class="hljs-string">&apos;cookie&apos;</span>)||<span class="hljs-string">&apos;&apos;</span>
    }
});
</code></pre>
<h3 id="t12220.7 actions/session.js">20.7 actions/session.js <a href="#t12220.7 actions/session.js"> # </a></h3>
<p>src/store/actions/session.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    login(user) {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.post(<span class="hljs-string">&apos;/api/login&apos;</span>,user).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_SESSION,
                    <span class="hljs-attr">payload</span>:result.data.data
                });
            });
        }
    },
    logout() {
        <span class="hljs-comment">//https://github.com/reduxjs/redux-thunk/blob/master/src/index.js</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.get(<span class="hljs-string">&apos;/api/logout&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_SESSION,
                    <span class="hljs-attr">payload</span>:result.data.data
                });
            });
        }
    },
    getUser() {
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch,getState,request</span>) </span>{
            <span class="hljs-comment">//http://localhost:4000/api/users</span>
            <span class="hljs-keyword">return</span> request.get(<span class="hljs-string">&apos;/api/user&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
                dispatch({
                    <span class="hljs-attr">type</span>: types.SET_SESSION,
                    <span class="hljs-attr">payload</span>:result.data.data
                });
            });
        }
    }
}
</code></pre>
<h3 id="t12320.7 src/store/index.js">20.7 src/store/index.js <a href="#t12320.7 src/store/index.js"> # </a></h3>
<p>src/store/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">import</span> {createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> clientRequest <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../client/request&apos;</span>;
<span class="hljs-keyword">import</span> getServerRequest <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../server/request&apos;</span>;
<span class="hljs-keyword">import</span> thunk <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-thunk&apos;</span>;
<span class="hljs-keyword">import</span> logger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStore</span>(<span class="hljs-params">req</span>) </span>{
    <span class="hljs-keyword">return</span> createStore(reducers,applyMiddleware(thunk.withExtraArgument(getServerRequest(req)),logger));
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClientStore</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> initState=<span class="hljs-built_in">window</span>.context.state;
    <span class="hljs-keyword">return</span> createStore(reducers,initState,applyMiddleware(thunk.withExtraArgument(clientRequest),logger));
}
</code></pre>
<h2 id="t12421. 404">21. 404 <a href="#t12421. 404"> # </a></h2>
<h3 id="t12521.1 src/routes.js">21.1 src/routes.js <a href="#t12521.1 src/routes.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User&apos;</span>;
<span class="hljs-keyword">import</span> UserList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/User/components/UserList&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Counter&apos;</span>;
<span class="hljs-keyword">import</span> Login <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Login&apos;</span>;
<span class="hljs-keyword">import</span> Logout <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Logout&apos;</span>;
<span class="hljs-keyword">import</span> Profile <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/Profile&apos;</span>;
<span class="hljs-keyword">import</span> NotFound <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/NotFound&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {
        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
        <span class="hljs-attr">component</span>: App,
        <span class="hljs-attr">loadData</span>:App.loadData,
        <span class="hljs-attr">routes</span>: [
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
                <span class="hljs-attr">component</span>: Home,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/home&apos;</span>,
                <span class="hljs-attr">loadData</span>:Home.loadData
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">component</span>: User,
                <span class="hljs-attr">key</span>: <span class="hljs-string">&apos;/user&apos;</span>,
                <span class="hljs-attr">routes</span>: [
                    {
                        <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/user/list&apos;</span>,
                        <span class="hljs-attr">component</span>: UserList,
                        <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/user/list&apos;</span>
                    }
                ]
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/counter&apos;</span>,
                <span class="hljs-attr">component</span>: Counter,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;counter&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/login&apos;</span>,
                <span class="hljs-attr">component</span>: Login,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/login&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/logout&apos;</span>,
                <span class="hljs-attr">component</span>: Logout,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/logout&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/profile&apos;</span>,
                <span class="hljs-attr">component</span>: Profile,
                <span class="hljs-attr">key</span>:<span class="hljs-string">&apos;/profile&apos;</span>,
                <span class="hljs-attr">exact</span>: <span class="hljs-literal">true</span>
            },
            {
                <span class="hljs-attr">component</span>: NotFound
            }
        ]
    }

]
<span class="hljs-comment">/**
export default (
    &lt;Fragment&gt;
        &lt;Route path=&quot;/&quot; exact component={Home}&gt;&lt;/Route&gt;
        &lt;Route path=&quot;/counter&quot; exact component={Counter}&gt;&lt;/Route&gt;
    &lt;/Fragment&gt;
)
*/</span>
</code></pre>
<h3 id="t12621.2 src/server/render.js">21.2 src/server/render.js <a href="#t12621.2 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore(req);
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes=matchRoutes(routes,req.path);
    <span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData)
            promises.push(item.route.loadData(store));
    });
    <span class="hljs-built_in">Promise</span>.all(promises).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> context={};
        <span class="hljs-keyword">const</span> content=renderToString(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{context}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    {renderRoutes(routes)}
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        );
        <span class="hljs-keyword">if</span> (context.notFound) {
            res.status(<span class="hljs-number">404</span>);
        }
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1c11110a0d0a0c1f0e3e4d504d5049">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
    });
}
</code></pre>
<h3 id="t12721.3 NotFound/index.js">21.3 NotFound/index.js <a href="#t12721.3 NotFound/index.js"> # </a></h3>
<p>src/containers/NotFound/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NotFound</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    componentWillMount() {
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.props.staticContext) {
            <span class="hljs-keyword">this</span>.props.staticContext.notFound=<span class="hljs-literal">true</span>;
        }
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>404<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h2 id="t12822. 301">22. 301 <a href="#t12822. 301"> # </a></h2>
<h3 id="t12922.1 server/render.js">22.1 server/render.js <a href="#t12922.1 server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {StaticRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../routes&apos;</span>;
<span class="hljs-keyword">import</span> {renderToString} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> {matchRoutes,renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> {getStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/App&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-keyword">let</span> store=getStore(req);
    <span class="hljs-comment">/**
    let matchedRoutes=routes.filter(route =&gt; {
        return matchPath(req.path,route);
    });
    */</span>
    <span class="hljs-keyword">let</span> matchedRoutes=matchRoutes(routes,req.path);
    <span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData)
            promises.push(item.route.loadData(store));
    });
    <span class="hljs-built_in">Promise</span>.all(promises).then(<span class="hljs-function"><span class="hljs-params">result</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> context={};
        <span class="hljs-keyword">const</span> content=renderToString(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">StaticRouter</span> <span class="hljs-attr">context</span>=<span class="hljs-string">{context}</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{req.path}</span>&gt;</span>
                    {renderRoutes(routes)}
                <span class="hljs-tag">&lt;/<span class="hljs-name">StaticRouter</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        );
        <span class="hljs-keyword">if</span> (context.action == <span class="hljs-string">&apos;REPLACE&apos;</span>) {
            <span class="hljs-keyword">return</span> res.redirect(<span class="hljs-number">301</span>,context.url);
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (context.notFound) {
            res.status(<span class="hljs-number">404</span>);
        }
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc9c4c4dfd8dfd9cadbeb988598859c">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
    });
}
</code></pre>
<h2 id="t13023. promise.all">23. promise.all <a href="#t13023. promise.all"> # </a></h2>
<h3 id="t13123.1 src/server/render.js">23.1 src/server/render.js <a href="#t13123.1 src/server/render.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> promises=[];
    matchedRoutes.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (item.route.loadData) {
            <span class="hljs-keyword">let</span> promise=<span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve,reject</span>) </span>{
                <span class="hljs-keyword">return</span> item.route.loadData(store).then(resolve,resolve);
            });
            promises.push(promise);
        }    
    });
</code></pre>
<h2 id="t13224. &#x4F7F;&#x7528;CSS">24. &#x4F7F;&#x7528;CSS <a href="#t13224. &#x4F7F;&#x7528;CSS"> # </a></h2>
<h3 id="t13324.1 Header/index.js">24.1 Header/index.js <a href="#t13324.1 Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./index.css&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Header</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-inverse navbar-fixed-top&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;SSR<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;navbar&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;collapse navbar-collapse&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              {
                                !this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/login&quot;</span>&gt;</span>&#x767B;&#x5F55;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              }
                              {
                                this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/logout&quot;</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/profile&quot;</span>&gt;</span>&#x4E2A;&#x4EBA;&#x4E2D;&#x5FC3;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                              }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        {
                            this.props.user&amp;&amp;(
                                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav navbar-right&quot;</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x6B22;&#x8FCE; <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.user}</span>&gt;</span>{this.props.user.username}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                            )
                         }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>state.session
)(Header);
</code></pre>
<h3 id="t13424.2 src/containers/App.js">24.2 src/containers/App.js <a href="#t13424.2 src/containers/App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions/session&apos;</span>;
<span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./App.css&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function">(<span class="hljs-params">store</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> store.dispatch(actions.getUser());
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span>/&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.app}</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(this.props.route.routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t13524.3 webpack.client.js">24.3 webpack.client.js <a href="#t13524.3 webpack.client.js"> # </a></h3>
<p>webpack.client.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>:{
        <span class="hljs-attr">rules</span>:[
            {
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.css$/</span>,
                <span class="hljs-attr">use</span>: [
                    <span class="hljs-string">&apos;style-loader&apos;</span>,
                    {
                        <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;css-loader&apos;</span>,
                        <span class="hljs-attr">options</span>: {
                            <span class="hljs-attr">modules</span>: <span class="hljs-literal">true</span>,
                            <span class="hljs-attr">localIdentName</span>:<span class="hljs-string">&apos;[name]_[local]_[hash:base64:5]&apos;</span>
                        }
                    }
                ]
            }
        ]
    }
</code></pre>
<h3 id="t13624.4 webpack.server.js">24.4 webpack.server.js <a href="#t13624.4 webpack.server.js"> # </a></h3>
<p>webpack.server.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>:{
        <span class="hljs-attr">rules</span>:[
            {
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.css$/</span>,
                <span class="hljs-attr">use</span>: [
                    <span class="hljs-string">&apos;isomorphic-style-loader&apos;</span>,
                    {
                        <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;css-loader&apos;</span>,
                        <span class="hljs-attr">options</span>: {
                            <span class="hljs-attr">modules</span>: <span class="hljs-literal">true</span>,
                            <span class="hljs-attr">localIdentName</span>:<span class="hljs-string">&apos;[name]_[local]_[hash:base64:5]&apos;</span>
                        }
                    }
                ]
            }
        ]
    }
</code></pre>
<h3 id="t13724.5 Header/index.css">24.5 Header/index.css <a href="#t13724.5 Header/index.css"> # </a></h3>
<p>src/components/Header/index.css</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.user</span>{
    <span class="hljs-attribute">color</span>:red;
}
</code></pre>
<h3 id="t13824.6 src/containers/App.css">24.6 src/containers/App.css <a href="#t13824.6 src/containers/App.css"> # </a></h3>
<p>src/containers/App.css</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.app</span>{
    <span class="hljs-attribute">margin-top</span>:<span class="hljs-number">70px</span>;
}
</code></pre>
<h2 id="t13925. CSS&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;">25. CSS&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3; <a href="#t13925. CSS&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;"> # </a></h2>
<h3 id="t14025.1 Header/index.js">25.1 Header/index.js <a href="#t14025.1 Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+    componentWillMount() {</span>
<span class="hljs-addition">+        if (this.props.staticContext) {</span>
<span class="hljs-addition">+            this.props.staticContext.csses.push(styles._getCss());</span>
<span class="hljs-addition">+        }</span>
<span class="hljs-addition">+    }</span>
</code></pre>
<h3 id="t14125.2 containers/App.js">25.2 containers/App.js <a href="#t14125.2 containers/App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+    componentWillMount() {</span>
<span class="hljs-addition">+        if (this.props.staticContext) {</span>
<span class="hljs-addition">+            this.props.staticContext.csses.push(styles._getCss());</span>
<span class="hljs-addition">+        }</span>
<span class="hljs-addition">+    }</span>
</code></pre>
<h3 id="t14225.3 server/render.js">25.3 server/render.js <a href="#t14225.3 server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-diff">let context={csses:[]};
        const content=renderToString(
            &lt;Provider store={store}&gt;
                &lt;StaticRouter context={context} location={req.path}&gt;
                    {renderRoutes(routes)}
                &lt;/StaticRouter&gt;
            &lt;/Provider&gt;
        );
<span class="hljs-addition">+        let cssStr=&apos;&apos;;</span>
<span class="hljs-addition">+        if (context.csses.length&gt;0) {</span>
<span class="hljs-addition">+            cssStr=context.csses.join(&apos;\r\n&apos;);</span>
<span class="hljs-addition">+        }</span>
        if (context.action == &apos;REPLACE&apos;) {
            return res.redirect(301,context.url);
        } else if (context.notFound) {
            res.status(404);
        }
        res.send(`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56343939222522243726166578657861">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
<span class="hljs-addition">+                &lt;style&gt;${cssStr}&lt;/style&gt;</span>
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;${content}&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:${JSON.stringify(store.getState())}
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `);
</code></pre>
<h2 id="t14326. &#x4F18;&#x5316;CSS&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;">26. &#x4F18;&#x5316;CSS&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3; <a href="#t14326. &#x4F18;&#x5316;CSS&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;"> # </a></h2>
<h3 id="t14426.1 Header/index.js">26.1 Header/index.js <a href="#t14426.1 Header/index.js"> # </a></h3>
<p>src/components/Header/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./index.css&apos;</span>;
<span class="hljs-keyword">import</span> withStyles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../withStyles&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Header</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-inverse navbar-fixed-top&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;SSR<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;navbar&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;collapse navbar-collapse&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/user/list&quot;</span>&gt;</span>&#x7528;&#x6237;&#x5217;&#x8868;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              {
                                !this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/login&quot;</span>&gt;</span>&#x767B;&#x5F55;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                              }
                              {
                                this.props.user&amp;&amp;<span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/logout&quot;</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/profile&quot;</span>&gt;</span>&#x4E2A;&#x4EBA;&#x4E2D;&#x5FC3;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                              }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        {
                            this.props.user&amp;&amp;(
                                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav navbar-right&quot;</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x6B22;&#x8FCE; <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.user}</span>&gt;</span>{this.props.user.username}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                            )
                         }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>state.session
)(withStyles(Header,styles));
</code></pre>
<h3 id="t14526.2 src/containers/App.js">26.2 src/containers/App.js <a href="#t14526.2 src/containers/App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {renderRoutes} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-config&apos;</span>;
<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/Header&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions/session&apos;</span>;
<span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./App.css&apos;</span>;
<span class="hljs-keyword">import</span> withStyles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../withStyles&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    componentWillMount() {
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.props.staticContext) {
            <span class="hljs-keyword">this</span>.props.staticContext.csses.push(styles._getCss());
        }
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Header</span> <span class="hljs-attr">staticContext</span>=<span class="hljs-string">{this.props.staticContext}/</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.app}</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                      {renderRoutes(this.props.route.routes)}
                    <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
        )
    }
}
let Proxy=withStyles(App,styles);
Proxy.loadData=(store) =&gt; {
    return store.dispatch(actions.getUser());
}
export default Proxy;
</span></code></pre>
<h3 id="t14626.3 Home/index.js">26.3 Home/index.js <a href="#t14626.3 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/home&apos;</span>;
<span class="hljs-keyword">import</span> withStyles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../withStyles&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function">(<span class="hljs-params">store</span>) =&gt;</span> {
        <span class="hljs-comment">//dispatch&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;action</span>
        <span class="hljs-comment">//https://github.com/reduxjs/redux/blob/master/src/createStore.js</span>
        <span class="hljs-keyword">return</span> store.dispatch(actions.getHomeList());
    }
    <span class="hljs-comment">//componentDidMount&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x662F;&#x4E0D;&#x6267;&#x884C;&#x7684;</span>
    componentDidMount() {
        <span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.props.list.length==<span class="hljs-number">0</span>)
            <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        {
                            this.props.list.map(item =&gt; (
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            ))
                        }
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(Home);
</code></pre>
<h3 id="t14726.4 src/withStyles.js">26.4 src/withStyles.js <a href="#t14726.4 src/withStyles.js"> # </a></h3>
<p>src/withStyles.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">withStyles</span>(<span class="hljs-params">OriginalComponent,styles</span>) </span>{
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProxyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
        componentWillMount() {
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.props.staticContext) {
                <span class="hljs-keyword">this</span>.props.staticContext.csses.push(styles._getCss());
            }
        }
        render() {
            <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">OriginalComponent</span> {<span class="hljs-attr">...this.props</span>}/&gt;</span>
        }
    }
    return ProxyComponent;
}
</span></code></pre>
<h2 id="t14827. SEO&#x4F18;&#x5316;">27. SEO&#x4F18;&#x5316; <a href="#t14827. SEO&#x4F18;&#x5316;"> # </a></h2>
<h3 id="t14927.1 Home/index.js">27.1 Home/index.js <a href="#t14927.1 Home/index.js"> # </a></h3>
<p>src/containers/Home/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> {Helmet} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-helmet&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../store/actions/home&apos;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">static</span> loadData=<span class="hljs-function">(<span class="hljs-params">store</span>) =&gt;</span> {
        <span class="hljs-comment">//dispatch&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;action</span>
        <span class="hljs-comment">//https://github.com/reduxjs/redux/blob/master/src/createStore.js</span>
        <span class="hljs-keyword">return</span> store.dispatch(actions.getHomeList());
    }
    <span class="hljs-comment">//componentDidMount&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x662F;&#x4E0D;&#x6267;&#x884C;&#x7684;</span>
    componentDidMount() {
        <span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.props.list.length==<span class="hljs-number">0</span>)
            <span class="hljs-keyword">this</span>.props.getHomeList();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Helmet</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&#x9996;&#x9875;&#x6807;&#x9898;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;description&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;&#x9996;&#x9875;&#x63CF;&#x8FF0;&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">meta</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">Helmet</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                            {
                                this.props.list.map(item =&gt; (
                                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                ))
                            }
                        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.home,
    actions
)(Home);
</code></pre>
<h3 id="t15027.2 src/server/render.js">27.2 src/server/render.js <a href="#t15027.2 src/server/render.js"> # </a></h3>
<p>src/server/render.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {Helmet} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-helmet&apos;</span>;
<span class="hljs-keyword">let</span> helmet=Helmet.renderStatic();

        <span class="hljs-keyword">let</span> cssStr=<span class="hljs-string">&apos;&apos;</span>;
        <span class="hljs-keyword">if</span> (context.csses.length&gt;<span class="hljs-number">0</span>) {
            cssStr=context.csses.join(<span class="hljs-string">&apos;\r\n&apos;</span>);
        }
        <span class="hljs-keyword">if</span> (context.action == <span class="hljs-string">&apos;REPLACE&apos;</span>) {
            <span class="hljs-keyword">return</span> res.redirect(<span class="hljs-number">301</span>,context.url);
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (context.notFound) {
            res.status(<span class="hljs-number">404</span>);
        }
        res.send(<span class="hljs-string">`
        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;
            &lt;head&gt;
                &lt;meta charset=&quot;UTF-8&quot;&gt;
                &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
                &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
                <span class="hljs-subst">${helmet.title.toString()}</span>
                <span class="hljs-subst">${helmet.meta.toString()}</span>
                &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5954544f484f495a4b7b081508150c">[email&#160;protected]</a>/dist/css/bootstrap.min.css&quot; &gt;
                &lt;style&gt;<span class="hljs-subst">${cssStr}</span>&lt;/style&gt;
                &lt;title&gt;&#x73E0;&#x5CF0;SSR&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div id=&quot;root&quot;&gt;<span class="hljs-subst">${content}</span>&lt;/div&gt;
                &lt;script&gt;
                  window.context = {
                      state:<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(store.getState())}</span>
                  }
                &lt;/script&gt;
                &lt;script src=&quot;/index.js&quot;&gt;&lt;/script&gt;
            &lt;/body&gt;
        &lt;/html&gt;
        `</span>);
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
